#ifndef FLORALOGGER_H
#define FLORALOGGER_H

#include <QString>
#include <QDebug>
#include <QFile>
#include <QTextStream>
#include <QDateTime>
#include <QWidget>
#include <QTextEdit>
#include <QFont>
enum LogLevel {
    DEBUG_LOG,
    INFO_LOG,
    WARN_LOG,
    ERROR_LOG
};

class FloraLogger : public QWidget
{
    Q_OBJECT

public:
    static FloraLogger& instance()
    {
        static FloraLogger instance;
        return instance;
    }

    void debug(const QString& message);
    void info(const QString& message);
    void warn(const QString& message);
    void error(const QString& message);

    void openLogFile();
    void closeLogFile();
    void saveLogToFile();

    void enableLogToFile(bool enable) { logToFile = enable; }
    void enableLogWindow(bool enable) { showLogWindow = enable; }
    void embedInWidget(QWidget *parent);
    void setFont(const QFont& font);

private:
    FloraLogger();
    ~FloraLogger();

    QFile logFile;
    QTextStream logStream;
    bool logToFile;
    bool showLogWindow;
    QWidget *logWindowWidget;
    QTextEdit *logView;

    void setupLogFile();
    void initLogWindow();
    void logMessage(const QString& message, LogLevel level);
signals:
    void haveMes(QString);
};



#ifdef DEVICE_DEBUG
    LOG_DEV_DEBUG() qDebug()<<(QString("[DEBUG] [%1] [%2] [%3] [%4] ").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(QString(__FILE__).section('/', -1).section('\\', -1)).arg(__FUNCTION__).arg(__LINE__))
#else
    #define LOG_DEV_DEBUG() QNoDebug()
#endif
#define LOG_DEBUG qDebug()<<(QString("[DEBUG] [%1] [%2] [%3] [%4] ").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(QString(__FILE__).section('/', -1).section('\\', -1)).arg(__FUNCTION__).arg(__LINE__))
#define LOG_INFO(msg) FloraLogger::instance().info(QString("[INFO] [%1] [%2] [%3] [%4] %5").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(QString(__FILE__).section('/', -1).section('\\', -1)).arg(__FUNCTION__).arg(__LINE__).arg(msg))
#define LOG_WARN(msg) FloraLogger::instance().warn(QString("[WARNING] [%1] [%2] [%3] [%4] %5").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(QString(__FILE__).section('/', -1).section('\\', -1)).arg(__FUNCTION__).arg(__LINE__).arg(msg))
#define LOG_ERROR(msg) FloraLogger::instance().error(QString("[ERROR] [%1] [%2] [%3] [%4] %5").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")).arg(QString(__FILE__).section('/', -1).section('\\', -1)).arg(__FUNCTION__).arg(__LINE__).arg(msg))
#endif // FLORALOGGER_H
